package com.route.system.converter;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import com.route.system.entity.RpsStation;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;

/**
 *
 * @author cihan
 */
@FacesConverter(value="StationConverter")
public class StationConverter implements Converter{

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        try {
            if(value != null) {
                Context c = new InitialContext();
                EntityManager em = Persistence.createEntityManagerFactory("RPS_PU").createEntityManager();
                RpsStation station = em.find(RpsStation.class, Integer.valueOf(value));
                return station;
            }
        } catch (Exception e) {
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return value.toString();
    }
    
}
